<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Job Management</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }

        form {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input[type="text"], input[type="number"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
        }

        button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        #result {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>

<h1>Job Management</h1>

<!-- Register Job Form -->
<form id="registerJobForm">
    <h2>Register Job</h2>
    <label for="jobName">Job Name:</label>
    <input type="text" id="jobName" name="jobName" required>
    <label for="jobCount">Job Count:</label>
    <input type="number" id="jobCount" name="jobCount" required>
    <button type="submit">Register Job</button>
</form>

<!-- Query Job Form -->
<form id="queryJobForm">
    <h2>Query Job</h2>
    <label for="queryJobName">Job Name (optional):</label>
    <input type="text" id="queryJobName" name="jobName">
    <button type="submit">Query Job</button>
</form>

<!-- Result Display -->
<div id="result"></div>

<script>
    document.getElementById('registerJobForm').addEventListener('submit', function (event) {
        event.preventDefault();
        const jobName = document.getElementById('jobName').value;
        const jobCount = document.getElementById('jobCount').value;
        document.getElementById('result').innerText = "loading";
        fetch(`/api/register?jobName=${encodeURIComponent(jobName)}&jobCount=${encodeURIComponent(jobCount)}`, {
            method: 'GET'
        })
            .then(response => response.text())
            .then(data => {
                document.getElementById('result').innerText = JSON.stringify(data, null, 2);
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('result').innerText = 'An error occurred while registering the job.';
            });
    });

    document.getElementById('queryJobForm').addEventListener('submit', function (event) {
        event.preventDefault();
        const jobName = document.getElementById('queryJobName').value;
        document.getElementById('result').innerText = "loading";
        let event_chain = jobName
            ? fetch(
                `/api/query?jobName=${encodeURIComponent(jobName)}`
                , {
                    method: 'GET'
                })
            : fetch(`/api/query`, {
                method: 'GET'
            })
        event_chain.then(response => response.json())
            .then(data => {
                if (jobName) {
                    document.getElementById('result').innerText = JSON.stringify(data, null, 2);
                } else {
                    let table = document.createElement("table");
                    let header = table.createTHead();
                    let row = header.insertRow();
                    row.insertCell().innerHTML = "职位名称";
                    row.insertCell().innerHTML = "职位数量";
                    // a map is received
                    for (let [key, value] of Object.entries(data)) {
                        let row = table.insertRow();
                        let cell1 = row.insertCell();
                        let cell2 = row.insertCell();
                        cell1.innerHTML = key;
                        cell2.innerHTML = value;
                    }
                    document.getElementById('result').innerHTML = "";
                    document.getElementById('result').appendChild(table);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                document.getElementById('result').innerText = 'An error occurred while querying the job.';
            });
    });
</script>

</body>
</html>
